package com.kongjs.application.controller.page;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.kongjs.application.enums.PostType;
import com.kongjs.application.model.entity.Posts;
import com.kongjs.application.model.entity.Terms;
import com.kongjs.application.service.IPostsService;
import com.kongjs.application.service.ITermsService;
import com.kongjs.common.rest.annotation.Page;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Page
@Controller
@RequestMapping
public class IndexPageController {
    @Resource
    private IPostsService postsService;
    @Resource
    private ITermsService termsService;

    @GetMapping
    public String index(Model model, @RequestParam(required = false) String lastDate) {
        LocalDateTime localDateTime = null;
        try {
            if (StringUtils.hasText(lastDate)) {
                localDateTime = DateUtil.parse(lastDate).toLocalDateTime();
            }
        } catch (Exception e) {
            log.error("错误参数");
        }
        LambdaQueryWrapper<Posts> topicPostswrapper = new LambdaQueryWrapper<>();
        topicPostswrapper.eq(Posts::getPostType, PostType.topic.getValue());
        topicPostswrapper.orderByDesc(Posts::getPostModified);
        List<Posts> topicPosts = postsService.page(PageDTO.of(1, 4), topicPostswrapper).getRecords();
        model.addAttribute("topicPosts", topicPosts);

        LambdaQueryWrapper<Posts> lastPostswrapper = new LambdaQueryWrapper<>();
        lastPostswrapper.eq(Posts::getPostType, PostType.article.getValue())
                .gt(!ObjectUtils.isEmpty(localDateTime), Posts::getPostDate, localDateTime)
                .orderByDesc(Posts::getPostDate);
        List<Posts> posts = postsService.list(lastPostswrapper);
        model.addAttribute("lastPosts", posts);

        LambdaQueryWrapper<Posts> hotPostswrapper = new LambdaQueryWrapper<>();
        hotPostswrapper.eq(Posts::getPostType, PostType.article.getValue())
                .orderByDesc(Posts::getPostViews);
        List<Posts> hotPosts = postsService.page(PageDTO.of(1, 10), hotPostswrapper).getRecords();
        model.addAttribute("hotPosts", hotPosts);


        LambdaQueryWrapper<Terms> categoryWrapper = new LambdaQueryWrapper<>();
        categoryWrapper.eq(Terms::getTermGroup, 0);
        LambdaQueryWrapper<Terms> tagWrapper = new LambdaQueryWrapper<>();
        tagWrapper.eq(Terms::getTermGroup, 1);
        model.addAttribute("categories", termsService.list(categoryWrapper));
        model.addAttribute("tags", termsService.list(tagWrapper));

        return "index.html";
    }
}
